package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mapper.EmployeeMapper;
import com.itheima.pojo.Employee;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

/**
 * @Author: 放学后海堤日记
 * @Date: 2023/4/13 14:33
 * @Desc:
 */
@Service
@Slf4j
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public Page<Employee> listAllEmp(Integer page, Integer pageSize, String name) {
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        if (name != null)
            wrapper.like("name", "%".concat(name).concat("%"));
        else
            wrapper = null;

        Page<Employee> page1 = new Page<>(page, pageSize);
        Page<Employee> employeePage = employeeMapper.selectPage(page1, wrapper);

        return employeePage;
    }

    @Override
    public int addEmp(Employee employee) {
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.eq("username", employee.getUsername());
        Employee selectOne = employeeMapper.selectOne(wrapper);
        if (selectOne != null)
            return 0;

        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

//        log.info("===============================");
//        log.info(employee.toString());

        int i = employeeMapper.insert(employee);
        return i;
    }

    @Override
    public int updateEmpInfo(Employee employee) {
        int i = employeeMapper.updateById(employee);

        return i;
    }

    @Override
    public Employee findEmpById(Long id) {
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        Employee employee = employeeMapper.selectOne(wrapper);

        return employee;
    }
}
